#pragma once

#include "SCServo/setup.h"

class SCServo;
class CServoMain;
class ServoPG : public CDialog
{
public:
	ServoPG();
	~ServoPG();
public:
	CEdit m_CommadCtl;

	CServoMain *pServoMainDlg;
	SCServo	*pServo;
	CCriticalSection *pCs;
	CSetup *setup;

	CString servoMem[254][SERVO_MEM_LEN];
	UINT8 servoInfo[SERVO_INFO_LEN];

	UCHAR nMemData[254];
	int servoID;
	int	memLen;
	int Refresh;
	int	timeID;

	CString timeOutStr;
	CString normalStr;
	CString overVolStr;
	CString overCurrentStr;
	CString overHeatStr;
	CString overLoadStr;
	CString overSenStr;
	CString memStr;
	CString addrStr;
	CString areaStr;
	CString RWStr;
	CString valueStr;
	CString ronlyStr;
	CString saveStr;
	CString setStr;
	CString lockStr;
	CString baudRateStr;
	CString ackLevleStr;
	CString memLoadStr;
	CString memSaveStr;
	CString memSetStr;
	CString ErrStr;
	CString SucStr;
	CString memErrStr;
	CString memLargerThanStr;
	CString memLessThanStr;
	CString IDStr;
	CString ParrInfoErr;
	CString OnlineIDErr;
protected:
	DECLARE_MESSAGE_MAP()
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	virtual BOOL OnInitDialog();
	virtual void OnOK();
	virtual void OnCancel();
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnBnClickedBtnSave();
	afx_msg void OnBnClickedBtnLoad();
	afx_msg void OnBnClickedBtnSet();
	afx_msg void OnNMClickListMem(NMHDR *pNMHDR, LRESULT *pResult);
public:
	void WriteComEdit(CString str);
	int writeMem(CString Mem[][SERVO_MEM_LEN], int nLen, UCHAR *nData, int End);
	int readUserEprom(CString Mem[][SERVO_MEM_LEN], UINT8 *nData);
	int getDefaultMemi(CString Mem[][SERVO_MEM_LEN]);
	int getDefaultSt(CString Mem[][SERVO_MEM_LEN], int Memi);
	int readDefaultEprom(CString Mem[][SERVO_MEM_LEN], int Memi, int epromSt, UINT8 *nData);
	int getMemLen(CString Mem[][SERVO_MEM_LEN]);
	int findMemAddr(CString Mem[][SERVO_MEM_LEN], CString memStr);
	void SetCurID(int ID, UINT8 Info[]);
	void TimeStart();
	void TimeStop();
	int memWrite(int addrST, int len, UINT16 val, int lock);
	int memUserWrite(int addrST, int len, UINT8 *nData, int lock);
	int memDefaultWrite(int addrST, int len, UINT8 *nData, int lock);
	void resetList(CString Mem[][SERVO_MEM_LEN]);
	void resetList();
};